﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Blo;
using Dict.DataModel;
using ProtoBuf.Message;

namespace UI
{
    public class UIActivityVipBenefits : UIActivityTemplate
    {
        public GameObject dailyTab;
        public GameObject weeklyTab;
        public UIToggle tabDailyBtn;
        public UIToggle tabWeeklyBtn;

        /// <summary>
        /// 每日福利
        /// </summary>
        public VipBenefitDailyReward curLevelReward;
        public VipBenefitDailyReward nextLevelReward;
        public GameObject nextLevelTipRoot;
        public GameObject levelMaxRoot;
        public UILabel labVipLevel;
        public UILabel labDiamondNum;
        public UILabel labNextTip;

        /// <summary>
        /// 每周礼包
        /// </summary>
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public GameObject weeklyItemPref;

        List<ActivityDailyBoonItem> dailyList;
        List<ActivityShopItem> weeklyList;

        public override void Show()
        {
            base.Show();

            this.activityType = ActivityType.VIPBenefits;

            curLevelReward.vipBenefits = this;
            nextLevelReward.vipBenefits = this;

            CSGetVipBoonInfo msg = new CSGetVipBoonInfo();
            NetAPI.SendHttp(OpDefine.CSGetVipBoonInfo, msg, OnMsgRecv);
        }

        void OnMsgRecv(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                SCGetVipBoonInfo msg = packet.kBody as SCGetVipBoonInfo;
                dailyList = msg.daily;
                weeklyList = msg.week;

                weeklyList.Sort(delegate (ActivityShopItem a, ActivityShopItem b)
                {
                    if (a.index < b.index)
                        return -1;
                    else if (a.index > b.index)
                        return 1;
                    return 0;
                });
                
                tabDailyBtn.Set(true);
                OnTabBtn(tabDailyBtn.gameObject);
            }
        }

        public void UpdateDailyTab(List<ActivityDailyBoonItem> list)
        {
            if (list != null)
                dailyList = list;

            int vip = StaticData.playerData.vip;

            ActivityDailyBoonItem curLevelboon = null;
            ActivityDailyBoonItem nextLevelboon = null;

            foreach(var item in dailyList)
            {
                if (item.vip == vip)
                    curLevelboon = item;
                else if (item.vip == vip + 1)
                    nextLevelboon = item;
            }
            if (nextLevelboon == null)
                nextLevelboon = curLevelboon;

            curLevelReward.SetRewardInfo(curLevelboon);
            nextLevelReward.SetRewardInfo(nextLevelboon,true);

            labVipLevel.text = StaticData.playerData.vip.ToString();
            TableVipPerks nextLevelData = DictVIPPerkBlo.GetTableVip(vip + 1);
            if(nextLevelData == null)
            {
                nextLevelTipRoot.SetActive(false);
                levelMaxRoot.SetActive(true);
            }
            else
            {
                nextLevelTipRoot.SetActive(true);
                levelMaxRoot.SetActive(false);

                labDiamondNum.text = (nextLevelData.chargeDiamond - StaticData.playerData.chargeDiamond).ToString();
                labNextTip.text = string.Format(TableTextBlo.GetText("canget_vip_x_gift_daily"), StaticData.playerData.vip + 1);

                Vector3 pos1 = labDiamondNum.transform.localPosition;
                Vector3 pos2 = labNextTip.transform.localPosition;
                labNextTip.transform.localPosition = new Vector3(pos1.x + labDiamondNum.width + 10, pos2.y, pos2.z);
            }
        }

        public void UpdateWeeklyTab(List<ActivityShopItem> list)
        {
            if(list != null)
            {
                weeklyList = list;
                weeklyList.Sort(delegate (ActivityShopItem a, ActivityShopItem b)
                {
                    if (a.index < b.index)
                        return -1;
                    else if (a.index > b.index)
                        return 1;
                    return 0;
                });
            }

            foreach(var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();

            for(int i = 0; i < weeklyList.Count; i++)
            {
                GameObject obj = Instantiate(weeklyItemPref);
                obj.SetActive(true);
                obj.GetComponent<VipBenefitWeeklyReward>().SetReward(weeklyList[i],this);
                simpleGrid.AddChild(obj);
            }
            if(list == null)
            {
                scrollView.ResetPosition();
            }
            
        }

        protected override void PrepareData()
        {

        }

        public void OnTabBtn(GameObject obj)
        {
            if(obj.name == "daily")
            {
                dailyTab.SetActive(true);
                weeklyTab.SetActive(false);

                UpdateDailyTab(null);
            }
            else if(obj.name == "weekly")
            {
                weeklyTab.SetActive(true);
                dailyTab.SetActive(false);

                UpdateWeeklyTab(null);
            }
        }
    }
}

